﻿using Klear.Library.Helpers;

namespace Klear.Library.Services;

public class DataBaseVersionStorage : IDataStorageTemplate
{
    //整型存取
    public void Set(string key, int value) => Set(key, value.ToString());

    public int Get(string key, int defaultValue)
    {
        return int.TryParse(Get(key, string.Empty), out var value)
            ? value
            : defaultValue;
    }
        
    //字符串存取
    public void Set(string key, string value) =>
        File.WriteAllText(PathHelper.GetLocalFilePath(key), value);
    public string Get(string key, string defaultValue) =>
        File.Exists(PathHelper.GetLocalFilePath(key)) 
            ? File.ReadAllText(PathHelper.GetLocalFilePath(key)) 
            : defaultValue;
    
    
    //日期存取
    public void Set(string key, DateTime value) => Set(key, value.ToString());

    public DateTime Get(string key, DateTime defaultValue) =>
        DateTime.TryParse(Get(key, ""), out var value) ? value : defaultValue;
}